package com.kong.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yukong
 * @date 2018/8/22
 * @description rabbitmq配置类
 */
@Configuration
public class RabbitConfig {

    /**
     * 定义队列名
     */
    private final static String STRING = "STRING";


    /**
     * 定义string队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(STRING);

    }

    /**
     * 广播模式
     *
     * @return
     */
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("FANOUT_EXCHANGE");
    }

    /**
     * 针对消费者配置
     * 1. 设置交换机类型
     * 2. 将队列绑定到交换机
     * FanoutExchange: 将消息分发到所有的绑定队列，无routingkey的概念
     * HeadersExchange ：通过添加属性key-value匹配
     * DirectExchange:按照routingkey分发到指定队列
     * TopicExchange:多关键字匹配
     */
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange("EXCHANGE_A");
    }

    @Bean
    public Binding binding() {

        return BindingBuilder.bind(queue()).to(defaultExchange()).with("FANOUT_EXCHANGE");
    }
}
